from .common import *

# draw a line chart.
def DrawLineFig(x_tick_set, x_tick_pos, x_ticklabels, y_value_set, legend_labels,
                x_label, y_label, x_min, x_max, y_min, y_max,
                folder, filename, allow_legend, out_format='eps',
                color_map=COLOR_MAP):
    """DrawLineFig: Function used to draw n lines.
    \t Usage:
    \t        x_tick_set: List of x values for each line.
    \t        x_ticklabels: Labels for each x tick, #x_ticklabels = maximum len(x_tick_set[i]).
    \t        y_value_set: y_values for each line.
    \t        legend_labels: Legend labels.
    \t        x_label: Label for the x axis.
    \t        y_label: Label for the x axis.
    \t        x_{min, max}: The min and max value along the x axis.
    \t        y_{min, max}: The min and max value along the y axis.
    \t        folder: The folder name.
    \t        filename: The filename.
    \t        allow_legend: The legend flag.
    \t        color_map: The color map.
    """
    if allow_legend:
        assert(len(legend_labels) >= len(y_value_set))

    # you may change the figure size on your own.
    fig = plt.figure(figsize=(8, 3.2))
    figure = fig.add_subplot(111)

    assert(len(x_tick_pos) == len(x_ticklabels))

    lines = [None] * (len(y_value_set))
    for i in range(len(y_value_set)):
        if len(x_tick_set) == 1:
            lines[i], = figure.plot(x_tick_set[0], y_value_set[i], color=color_map[i],
                                    linewidth=LINE_WIDTH, marker=MARKERS[i],
                                    markersize=MARKER_SIZE, \
                                    label= (allow_legend[i] if allow_legend else ""))
        else:
            lines[i], = figure.plot(x_tick_set[i], y_value_set[i], color=color_map[i],
                                    linewidth=LINE_WIDTH, marker=MARKERS[i],
                                    markersize=MARKER_SIZE, label=legend_labels[i])

    if allow_legend:
        if len(legend_labels) > 3:
            nrows = len(legend_labels) // 2
            ncols = math.ceil(len(legend_labels) / nrows)
            figure.legend(lines, legend_labels, prop=LEGEND_FP, loc='upper center',
                          ncol=ncols, bbox_to_anchor=(0.21, 0.13 * (nrows - 1), 0.45, 1.2),
                          shadow=False, frameon=False, borderaxespad=-0.22, handlelength=2,
                          labelspacing=0.2)
        else:
            ncols = len(legend_labels)
            figure.legend(lines, legend_labels, prop=LEGEND_FP, loc='upper center',
                          ncol=ncols, bbox_to_anchor=(0.45, 1.2), shadow=False,
                          frameon=False, borderaxespad=0., handlelength=2,
                          labelspacing=0.2)

    # plt.xticks(max_x_ticks)
    # you may control the limits on your own.
    plt.xlim(x_min, x_max)
    plt.ylim(y_min, y_max)
    plt.xticks(x_tick_pos, x_ticklabels)

    figure.grid(axis='y', color='gray')
    # figure.xaxis.set_major_locator(LinearLocator(6))
    figure.yaxis.set_major_locator(LinearLocator(6))

    figure.get_xaxis().set_tick_params(direction='in', pad=10)
    figure.get_yaxis().set_tick_params(direction='in', pad=10)

    plt.xlabel(x_label, fontproperties=LABEL_FP)
    plt.ylabel(y_label, fontproperties=LABEL_FP)

    if folder and filename:
        if not os.path.exists(folder):
            os.makedirs(folder)
        if out_format == 'eps':
            plt.savefig(folder + "/" + filename + ".eps", bbox_inches='tight',
                        format=out_format)
        elif out_format == 'pdf':
            plt.savefig(folder + '/' + filename + ".pdf", bbox_inches='tight',
                        format=out_format)
    else:
        plt.show()
